import type { AppItem, UserApps, SessionItem } from '@/types/userApp'
import { http, HttpResponse } from 'msw'
import type { LLMModel } from '@/types/llmModels'
const mockUserApps: UserApps = [
  {
    id: 1,
    title: '常用应用',
    createAt: '2023-01-01T00:00:00Z',
    updateAt: '2023-01-01T00:00:00Z',
    order: 1,
    appList: [
      {
        id: 101,
        title: '智能助手',
        desc: 'AI驱动的个人助理',
        avatar: 'https://cdn.seovx.com/?mom=302&t=' + Math.random(),
        lastActiveAt: '2023-04-15T10:30:00Z',
        createAt: '2023-01-15T08:00:00Z',
        updateAt: '2023-04-15T10:30:00Z',
        type: 'agent',
        isTop: true,
        groupId: 1,
        tags: [
          {
            name: 'Agent',
            type: 'info'
          },
          {
            name: 'GPT4',
            type: 'info'
          }
        ],
        modelId: 'gpt-4'
      },
      {
        id: 102,
        title: '任务管理器',
        desc: '高效的任务跟踪工具',
        avatar: 'https://cdn.seovx.com/?mom=302&t=' + Math.random(),
        lastActiveAt: '2023-04-14T16:45:00Z',
        createAt: '2023-02-01T09:00:00Z',
        updateAt: '2023-04-14T16:45:00Z',
        type: 'process',
        isTop: false,
        groupId: 1,
        tags: [
          {
            name: 'Agent',
            type: 'warning'
          },
          {
            name: 'GPT4',
            type: 'info'
          }
        ],
        modelId: 'gpt-4'
      }
    ]
  },
  {
    id: 2,
    title: '项目管理',
    createAt: '2023-01-01T00:00:00Z',
    updateAt: '2023-01-01T00:00:00Z',
    order: 2,
    appList: [
      {
        id: 201,
        title: '智能项目管理',
        desc: '高效的项目管理工具',
        avatar: 'https://cdn.seovx.com/?mom=302&t=' + Math.random(),
        lastActiveAt: '2023-04-13T12:20:00Z',
        createAt: '2023-03-01T10:00:00Z',
        updateAt: '2023-04-13T12:20:00Z',
        type: 'agent',
        isTop: false,
        groupId: 2,
        tags: [
          {
            name: 'Agent',
            type: 'warning'
          },
          {
            name: 'claude-instant-1',
            type: 'error'
          }
        ],
        modelId: 'claude-instant-1'
      }
    ]
  }
]

// 模拟会话数据
const mockSessions: SessionItem[] = [
  {
    id: 1,
    chatId: 'chat001',
    title: '项目讨论',
    desc: '关于新产品开发的讨论',
    isTop: true,
    isCollect: false,
    collectAt: '',
    createAt: '2023-04-10T09:00:00Z',
    updateAt: '2023-04-15T11:30:00Z',
    isAi: true,
    messageGroupList: [
      {
        id: 1,
        sessionId: 1,
        messageList: [
          {
            id: 1001,
            sender: 'user',
            timestamp: 1681545600000,
            type: 'text',
            content: '我们应该如何开始新产品的开发？',
            appId: 101,
            sessionId: 1,
            groupId: 1
          },
          {
            id: 1002,
            sender: 'bot',
            timestamp: 1681545660000,
            type: 'text',
            content:
              '首先，我们需要进行市场调研，确定目标用户群体和需求。然后制定产品规划和开发时间表。',
            appId: 101,
            sessionId: 1,
            groupId: 1
          }
        ]
      }
    ]
  }
]

export const handlers = [
  // 获取用户所有应用
  http.get('/mock/user/apps', () => {
    return HttpResponse.json({
      code: 200,
      data: mockUserApps,
      message: 'Success'
    })
  }),

  // 获取单个应用详情
  http.get('/mock/user/app/:id', ({ params }) => {
    const appId = parseInt(params.id as string)
    const app = mockUserApps.flatMap((group) => group.appList).find((app) => app.id === appId)
    if (app) {
      return HttpResponse.json({
        code: 200,
        data: app,
        message: 'Success'
      })
    }
    return HttpResponse.json(
      {
        code: 404,
        data: null,
        message: '应用未找到'
      },
      { status: 404 }
    )
  }),

  // 创建新应用
  http.post('/mock/user/app', async ({ request }) => {
    const newApp = (await request.json()) as AppItem
    newApp.id = Math.max(...mockUserApps.flatMap((group) => group.appList).map((app) => app.id)) + 1
    newApp.createAt = new Date().toISOString()
    newApp.updateAt = new Date().toISOString()
    newApp.lastActiveAt = new Date().toISOString()
    mockUserApps[0].appList.push(newApp)
    return HttpResponse.json(
      {
        code: 200,
        data: newApp,
        message: 'Success'
      },
      { status: 201 }
    )
  }),

  // 快捷创建应用
  http.post('/mock/user/app/quick', async () => {
    const quickApp: AppItem = {
      id: Math.max(...mockUserApps.flatMap((group) => group.appList).map((app) => app.id)) + 1,
      title: '新应用',
      desc: '请编辑应用描述',
      avatar: 'https://example.com/default-avatar.png',
      lastActiveAt: new Date().toISOString(),
      createAt: new Date().toISOString(),
      updateAt: new Date().toISOString(),
      type: 'agent',
      isTop: false
    }
    mockUserApps[0].appList.push(quickApp)
    return HttpResponse.json(
      {
        code: 200,
        data: quickApp,
        message: 'Success'
      },
      { status: 201 }
    )
  }),

  // 更新应用
  http.put('/mock/user/app/:id', async ({ params, request }) => {
    const appId = parseInt(params.id as string)
    const updatedApp = (await request.json()) as Partial<AppItem>
    const appGroup = mockUserApps.find((group) => group.appList.some((app) => app.id === appId))
    if (appGroup) {
      const appIndex = appGroup.appList.findIndex((app) => app.id === appId)
      if (appIndex !== -1) {
        appGroup.appList[appIndex] = {
          ...appGroup.appList[appIndex],
          ...updatedApp,
          updateAt: new Date().toISOString()
        }
        return HttpResponse.json({
          code: 200,
          data: appGroup.appList[appIndex],
          message: 'Success'
        })
      }
    }
    return HttpResponse.json(
      {
        code: 404,
        data: null,
        message: '应用未找到'
      },
      { status: 404 }
    )
  }),

  // 删除应用
  http.delete('/mock/user/app/:id', ({ params }) => {
    const appId = parseInt(params.id as string)
    const appGroup = mockUserApps.find((group) => group.appList.some((app) => app.id === appId))
    if (appGroup) {
      const appIndex = appGroup.appList.findIndex((app) => app.id === appId)
      if (appIndex !== -1) {
        appGroup.appList.splice(appIndex, 1)
        return HttpResponse.json(
          {
            code: 200,
            data: null,
            message: 'Success'
          },
          { status: 204 }
        )
      }
    }
    return HttpResponse.json(
      {
        code: 404,
        data: null,
        message: '应用未找到'
      },
      { status: 404 }
    )
  })
]
